using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Dtos;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Services
{
    /// <summary>
    /// 发布服务实现
    /// 提供内容发布和撤销发布的业务逻辑实现，包括发布历史记录和状态管理
    /// </summary>
    public class PublishService
    {
        /// <summary>
        /// 发布仓储接口
        /// </summary>
        private readonly Domain.Interfaces.IPublishRepository _publishRepository;
        
        /// <summary>
        /// 初始化发布服务实例
        /// </summary>
        /// <param name="publishRepository">发布仓储接口实例</param>
        public PublishService(Domain.Interfaces.IPublishRepository publishRepository)
        {
            _publishRepository = publishRepository;
        }

        /// <summary>
        /// 发布内容
        /// 将指定内容发布到生产环境，并记录发布历史
        /// </summary>
        /// <param name="request">发布请求DTO，包含内容ID等信息</param>
        /// <returns>发布是否成功</returns>
        public async Task<bool> PublishContentAsync(PublishRequestDto request)
        {
            // 发布内容逻辑
            await _publishRepository.PublishAsync(request.ContentId , null, null);
            // 可扩展：写入发布历史
            return true;
        }

        /// <summary>
        /// 撤销内容发布
        /// 将已发布的内容从生产环境撤回，并记录撤销历史
        /// </summary>
        /// <param name="request">撤销发布请求DTO，包含内容ID等信息</param>
        /// <returns>撤销是否成功</returns>
        public async Task<bool> RevokeContentAsync(RevokePublishRequestDto request)
        {
            // 撤销发布逻辑
            await _publishRepository.UnpublishAsync(request.ContentId);
            // 可扩展：写入撤销历史
            return true;
        }

        /// <summary>
        /// 获取发布历史记录
        /// 查询指定内容的所有发布和撤销操作历史
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <returns>发布历史记录列表</returns>
        public async Task<List<PublishHistoryDto>> GetPublishHistoryAsync(long contentId)
        {
            var history = await _publishRepository.GetPublishHistoryAsync(contentId );
            // DTO 映射
            return history.Select(p => new PublishHistoryDto
            {
                ContentId = p.ContentId ,
                ActionTime = p.PublishedAt ?? p.CreatedAt,
                ActionType = p.Status,
                Operator = p.UserId.ToString(),
                Status = p.Status
            }).ToList();
        }

        /// <summary>
        /// 获取发布状态
        /// 查询指定内容的当前发布状态和最后更新时间
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <returns>发布状态DTO</returns>
        public async Task<PublishStatusDto> GetPublishStatusAsync(long contentId)
        {
            var history = await _publishRepository.GetPublishHistoryAsync(contentId, 1, 1);
            var last = history.FirstOrDefault();
            return new PublishStatusDto
            {
                ContentId = contentId,
                Status = last?.Status ?? "draft",
                LastUpdateTime = last?.PublishedAt ?? last?.CreatedAt ?? DateTime.Now
            };
        }
    }
}